package com.sx.micowell.framework.adapter.binding.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.mvvm.until.GlideApp;
import com.sx.micowell.framework.adapter.binding.bean.BannerResponse;

import java.util.List;

public class MyBannerAdapter extends PagerAdapter {
    public List<BannerResponse> list;
    private final Context context;
    public boolean Noradius=false;


    public MyBannerAdapter(Context context, List<BannerResponse> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        BannerResponse bannerData = list.get(position);
        ImageView iv = new ImageView(context);
//        int dp = (int) context.getResources().getDimension(R.dimen.dp_118);
        if (iv.getLayoutParams() == null) {
            iv.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        } else {
            iv.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
            iv.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        if(!Noradius){
            GlideApp.with(context)
                    .load(bannerData.url)
                    .transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics())))
                    .into(iv);
        }else{
            GlideApp.with(context)
                    .load(bannerData.url)
                    .into(iv);
        }

        LinearLayout layout = new LinearLayout(context);
        layout.addView(iv);
        container.addView(layout);
       /* RxViewUntil.setClickShake(layout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bannerData.getmIntent()!=null){
                    context.startActivity(bannerData.getmIntent());
                }
            }
        });*/
        return layout;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

//    @Override
//    public float getPageWidth(int position) {
//        return 0.6f;
//    }

}
